#include "Slist.h"


void SList::InitList(List** p)
{
	*p = NULL;
}

void SList::PushBack(List** p, SLDataType n)
{
	List* ret = new List;
	if (ret == NULL)
	{
		perror("new");
		exit(-1);
	}
	ret->data = n;
	ret->next = NULL;
	if (*p == NULL)
	{
		*p = ret;
	}
	else
	{
		List* ph = *p;
		while (ph->next)
		{
			ph = ph->next;
		}
		ph->next = ret;
	}
}

void SList::PopBack(List** p)
{
	if (*p == NULL)
	{
		perror("NULL");
		exit(-1);
	}
	else if ((*p)->next == NULL)
	{
		delete* p;
		*p = NULL;
		return;
	}
	List* ret = *p;
	while (ret->next->next)
	{
		ret = ret->next;
	}
	delete ret->next;
	ret->next = NULL;
}

void SList::PushFront(List** p, SLDataType n)
{
	List* ret = new List;
	if (ret == NULL)
	{
		perror("new");
		exit(-1);
	}
	ret->data = n;
	ret->next = *p;
	*p = ret;
}

void SList::PopFront(List** p)
{
	List* ret = (*p)->next;
	delete *p;
	*p = ret;
}

void SList::Print(List* p)
{
	while (p)
	{
		cout << p->data << "->";
		p = p->next;
	}
	cout << endl;
}
